(function(){
    var Pipe = window.Pipe = function(){
        this.imageDown = game.R["pipeDown"];
        this.imageUp = game.R["pipeUp"];

        //管子的位置，是屏幕的最右边
        this.x = game.canvas.width;

        //上面管子的高度
        this.height1 = 100 + parseInt(Math.random() * 221);
        //间隙
        this.interspace = 160;
        //下面管子的高度就可以计算出来了
        this.height2 = game.canvas.height * 0.78 - this.interspace - this.height1;

        //是否已经成功通过
        this.alreadyPass = false;

        //将自己放入数组
        game.pipeArr.push(this);
    }
    Pipe.prototype.update = function(){
        this.x -= 2;

        //加分
        if(game.bird.R > this.x + 52 && !this.alreadyPass){
            //顺利通过了
            game.score ++;
            //标记为已经通过了
            this.alreadyPass = true;
        }

        //碰撞检测，检查自己有没有撞到小鸟
        if(game.bird.R > this.x && game.bird.L < this.x + 52){
            if(game.bird.T < this.height1 || game.bird.B > this.height1 + this.interspace){
                clearInterval(game.timer)
            }
        }


    }
    Pipe.prototype.render = function(){
        game.ctx.drawImage(this.imageDown,0,320-this.height1,52,this.height1,this.x,0,52,this.height1);
        game.ctx.drawImage(this.imageUp,0,0,52,this.height2,this.x,this.height1+this.interspace,52,this.height2);

    }
})();